//
//  ShareViewAA.m
//  one·一个  tyw
//
//  Created by tyw on 15/8/25.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "ShareViewAA.h"
#import "ShareLabel.h"
#import "DayNightManager.h"
#import "DayNightImgView.h"
#import "DataService.h"

@implementation ShareViewAA {
    UIButton* likeButton;               //收藏按钮
    UIButton* dayNightButton;           //日间夜间按钮
    UIButton* showSetButton;            //显示设置
    
    ShareLabel* likeLabel;              //加入收藏按钮下面的label    “加入收藏”  “已收藏"
    ShareLabel* dayNightLabel;          //日、夜间模式按钮下面的label  “夜间模式”  “日间模式”
    
    UIButton* cancelButton;             //取消按钮
    
    DayNightManager* manager;           //模式管理员
    
    NSDictionary* userInfo;             //通知传递过来的信息
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _createSubViews];
        
        manager = [DayNightManager sharedManager];
        
        //接收通知，每当一个collectionView  cell显示时就会接收到一次通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cellDisplayNotificationAction:) name:kCollectionCellWillDisplayNotification object:nil];
    }
    return self;
}

#pragma mark - subViews
- (void)_createSubViews {
    //分享到
    ShareLabel* shareLabel = [[ShareLabel alloc] initWithFrame:CGRectMake((kWidth-100)/2, 5, 100, 30)];
    shareLabel.text = @"分享到";
    shareLabel.font = [UIFont systemFontOfSize:15];
    [shareLabel loadLabelColor];
    [self addSubview:shareLabel];
    
    //scrollView  ，承载  分享到的各个平台 按钮
    CGFloat scrollViewHeight = 100;
    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, kWidth, scrollViewHeight)];
    //    scrollView.backgroundColor = [UIColor redColor];
    [self addSubview:scrollView];
    scrollView.contentSize = CGSizeMake(kWidth*1.2, scrollViewHeight);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.userInteractionEnabled = YES;
    
    //添加六个按钮，分享到各个平台
    NSArray* nameArray = @[@"新浪微博",@"微信好友",@"微信朋友圈",@"微信收藏",@"QQ好友",@"QQ空间"];
    NSArray* btnImgNameArray = @[@"share_sina@2x",@"share_wc@2x",@"share_timeline@2x",@"share_weChatFav@2x",@"share_qq@2x",@"share_qzone@2x"];
    NSArray* btnImgHNameArray = @[@"share_sinaH@2x",@"share_wcH@2x",@"share_timelineH@2x",@"share_weChatFavH@2x",@"share_qqH@2x",@"share_qzoneH@2x"];
    CGFloat width = kWidth/5-10;
    for (int i = 0; i < nameArray.count; i++) {
        UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(5+(10+width)*i, 5, width, width)];
        [scrollView addSubview:button];
        //        button.backgroundColor = [UIColor greenColor];
        [button setImage:[UIImage imageNamed:btnImgNameArray[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:btnImgHNameArray[i]] forState:UIControlStateHighlighted];
        button.tag = i+100;
        [button addTarget:self action:@selector(shareBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        ShareLabel* label = [[ShareLabel alloc] initWithFrame:CGRectMake(5+(10+width)*i, width+10, width, 15)];
        label.text = nameArray[i];
        [label loadLabelColor];
        //        label.backgroundColor = [UIColor yellowColor];
        [scrollView addSubview:label];
    }
    
    
    /**
     1、谁要使用block中传递的数据，谁就实现（定义）它。
     2、谁要把数据传递出去，谁就声明并且调用它。
     */
    
    
    //添加分割线1
    DayNightImgView* line1 = [[DayNightImgView alloc] initWithFrame:CGRectMake(5, 144, kWidth-10, 1)];
    [line1 setImgName:@"shareBgline"];
    [self addSubview:line1];
    
    //加入收藏
    likeButton = [[UIButton alloc] initWithFrame:CGRectMake(5+(10+width)*1, 150, width, width)];
    [likeButton setImage:[UIImage imageNamed:@"share_col@2x"] forState:UIControlStateNormal];
    [likeButton setImage:[UIImage imageNamed:@"share_coled@2x"] forState:UIControlStateSelected];
    [likeButton addTarget:self action:@selector(likeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:likeButton];
    //加入收藏文字
    likeLabel = [[ShareLabel alloc] initWithFrame:CGRectMake(5+(10+width)*1, 150+width, width, 15)];
    [likeLabel loadLabelColor];
    [self addSubview:likeLabel];
    //夜间模式
    dayNightButton = [[UIButton alloc] initWithFrame:CGRectMake(5+(10+width)*2, 150, width, width)];
    [dayNightButton setImage:[UIImage imageNamed:@"share_night@2x"] forState:UIControlStateNormal];
    [dayNightButton setImage:[UIImage imageNamed:@"share_day@2x"] forState:UIControlStateSelected];
    [dayNightButton addTarget:self action:@selector(dayNightTypeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:dayNightButton];
    //夜间模式文字
    dayNightLabel = [[ShareLabel alloc] initWithFrame:CGRectMake(5+(10+width)*2, 150+width, width, 15)];
    [dayNightLabel loadLabelColor];
    [self addSubview:dayNightLabel];

    //显示设置
    showSetButton = [[UIButton alloc] initWithFrame:CGRectMake(5+(10+width)*3, 150, width, width)];
    [showSetButton setImage:[UIImage imageNamed:@"share_font@2x"] forState:UIControlStateNormal];
    [showSetButton addTarget:self action:@selector(showSetBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:showSetButton];
    //显示设置文字
    ShareLabel* showSetLabel = [[ShareLabel alloc] initWithFrame:CGRectMake(5+(10+width)*3, 150+width, width, 15)];
    showSetLabel.text = @"显示设置";
    [showSetLabel loadLabelColor];
    [self addSubview:showSetLabel];
    
    //添加分割线2
    DayNightImgView* line2 = [[DayNightImgView alloc] initWithFrame:CGRectMake(5, 175+width, kWidth-10, 1)];
    [line2 setImgName:@"shareBgline"];
    [self addSubview:line2];
    
    //取消按钮
    cancelButton = [[UIButton alloc] initWithFrame:CGRectMake((kWidth-width)/2, 190+width, width, 30)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    //    _cancelButton.backgroundColor = [UIColor greenColor];
    [cancelButton setTitleColor:[UIColor colorWithRed:21.0/255 green:165.0/255 blue:246.0/255 alpha:1] forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithRed:25.0/255 green:197.0/255 blue:246.0/255 alpha:1] forState:UIControlStateHighlighted];
    [cancelButton addTarget:self action:@selector(cancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    //夜间模式按钮和文本 的 显示
    NSString* type = [manager getCurrentDayNightType];
    if ([type isEqualToString:kDayNightCurrentTypeIsDay]) {
        dayNightButton.selected = NO;
        dayNightLabel.text = @"夜间模式";
        self.backgroundColor = [UIColor whiteColor];
    } else {
        dayNightButton.selected = YES;
        dayNightLabel.text = @"日间模式";
        self.backgroundColor = [UIColor grayColor];
    }
}

#pragma mark - notificationAction
//celldisplay的时候，接收到通知调用的方法
//这里主要做以下事情
//查看通知传递过来的数据，根据是否收藏和点赞，刷新UI
- (void)cellDisplayNotificationAction:(NSNotification*)notification {
    userInfo = notification.userInfo;
    
    BOOL liked = [userInfo[@"liked"] boolValue];
    
    if (liked) {
        likeButton.selected = YES;
        likeLabel.text = @"取消收藏";
    } else {
        likeButton.selected = NO;
        likeLabel.text = @"加入收藏";
    }
}


#pragma mark - btnActions
- (void)shareBtnAction:(UIButton*)button {
    NSLog(@"分享到");
}

- (void)likeBtnAction:(UIButton*)button {
    button.selected = !button.selected;
    if (button.selected) {
        likeLabel.text = @"取消收藏";
        self.block(kLikeBlockIsLikedStr);
    } else {
        likeLabel.text = @"加入收藏";
        self.block(kLikeBlockNotLikedStr);
    }
    
    
    
    //网络请求,收藏或取消收藏
    //构造参数
    
    //如果是文章页
    if ([userInfo[@"strCt"] integerValue] == 1) {
        NSString* strCi = userInfo[@"strCi"];
        NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
        [params setObject:kStrUi forKey:@"strUi"];
        [params setObject:@0 forKey:@"strBr"];
        [params setObject:@0 forKey:@"strRtype"];
        [params setObject:@1 forKey:@"strMS"];
        [params setObject:@-1 forKey:@"strQi"];
        [params setObject:strCi forKey:@"strCi"];
        [params setObject:@1 forKey:@"strCt"];
        
        [DataService oneRequestUrl:kOtherCollectUrl httpMethod:@"POST" params:params block:^(id result) {
            if ([result isKindOfClass:[NSError class]]) {
                NSLog(@"网络请求 文章页收藏失败  %@", result);
            }
            
            NSLog(@"网络请求 文章页收藏成功");
        }];
        
    } else {        // 如果是问题页
        NSString* strCi = userInfo[@"strCi"];
        NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
        [params setObject:kStrUi forKey:@"strUi"];
        [params setObject:@0 forKey:@"strBr"];
        [params setObject:@0 forKey:@"strRtype"];
        [params setObject:@1 forKey:@"strMS"];
        [params setObject:@-1 forKey:@"strQi"];
        [params setObject:strCi forKey:@"strCi"];
        [params setObject:@2 forKey:@"strCt"];
        
        [DataService oneRequestUrl:kOtherCollectUrl httpMethod:@"POST" params:params block:^(id result) {
            if ([result isKindOfClass:[NSError class]]) {
                NSLog(@"网络请求 问题页收藏失败  %@", result);
            }
            
            NSLog(@"网络请求 问题页收藏成功");
        }];
    }
}
- (void)dayNightTypeBtnAction:(UIButton*)button {
    button.selected = !button.selected;
    if (button.selected) {
        dayNightLabel.text = @"日间模式";
        [manager changeCurrenDayNightType:kDayNightCurrentTypeIsNight];
        self.block(kDayNightBlockIsNightStr);
    } else {
        dayNightLabel.text = @"夜间模式";
        [manager changeCurrenDayNightType:kDayNightCurrentTypeIsDay];
        self.block(kDayNightBlockIsDayStr);
    }
}
- (void)showSetBtnAction:(UIButton*)button {
    self.block(kShowSetBlockStr);
}

- (void)cancelBtnAction:(UIButton*)button {
    self.block(kCancelBlockStr);
}

#pragma mark - dealloc
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kCollectionCellWillDisplayNotification object:nil];
}


@end
